package Player
{
	import FileSearch.FileSearch;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;

	public class Player
	{
			private var searcher:FileSearch;
			private var liste:Array;
			public var cur:int;
			private var myFile:File; 

			private var _myPausePosition:int;
			private var _mySound:Sound;
			private var _myChannel:SoundChannel;

			public var myLoaded:Boolean;
			public var myPlaying:Boolean;

						
			public function get myPausePosition():int { return _myPausePosition; }
			public function set myPausePosition(value:int):void { _myPausePosition = value; }			

			public function get myChannel():SoundChannel { return _myChannel; }
			public function set myChannel(value:SoundChannel):void { _myChannel = value; }			

			public function get mySound():Sound { return _mySound; }
			public function set mySound(value:Sound):void { _mySound = value; }			

			
			public function Player()
			{
				mySound = new Sound();
				myFile = File.desktopDirectory;
				myLoaded = false;
				myPlaying = false;
				var types:Array = ["mp3"];
				liste = new Array();
				searcher = new FileSearch(types, liste, mySound);
				cur=0;
			}
			
			
			
			
			public function play():Boolean{
				if (liste.length == 0) return false;			
				
				if (myPlaying)
				{
					if (myPausePosition){
						myChannel = mySound.play(myPausePosition);
						myPausePosition=0;
					}else{
						myPausePosition = myChannel.position;
						myChannel.stop();
					}								
				}
				else
				{

					myChannel=mySound.play();
					myChannel.addEventListener(Event.SOUND_COMPLETE,finish);
					myPausePosition=0;
					myPlaying=true;
					return true;
				}
			}
			
			public function finish(event:Event):void{
				myPausePosition=0;
				myPlaying=false;
				if (cur != liste.length - 1)
					next();
			}
			
			public function stop():Boolean{
				if (!myPlaying) return false;			
				myChannel.stop();
				myPausePosition=0;
				myPlaying=false;
				return true;
			}
			
			public function pause():void{
				if (!myPlaying) return;
				if (myPausePosition){
					myChannel = mySound.play(myPausePosition);
					myPausePosition=0;
				}else{
					myPausePosition = myChannel.position;
					myChannel.stop();
				}
			}
			
			public function browseDir(event:Event):void{
				myFile.addEventListener(Event.SELECT,onLoad);
				myFile.browseForDirectory("Directory Browse");	
			}
			
			public function onLoad(event:Event):void{
				searcher.search(myFile);
			}
			
			public function search(file:File):void{
				searcher.search(file);
			}
			
			public function next():void{
				cur++;
				if (cur == liste.length)
				{
					cur = 0;
				}
				
				myChannel.stop();
				myPausePosition=0;
				myPlaying=false;
				
				mySound = new Sound();
				mySound.load(new URLRequest((File (liste[cur])).nativePath));
				play();
			}
			
			public function pred():void{
				cur--;
				if (cur == -1)
				{
					cur = liste.length - 1;
				}
				
				myChannel.stop();
				myPausePosition=0;
				myPlaying=false;
				
				mySound = new Sound();
				mySound.load(new URLRequest((File (liste[cur])).nativePath));
				play();				
			}
			
	}
}